package demo_0416.homework;

import java.util.List;
import java.util.Scanner;

public class Main {
    private static final String PRINT_FILE = "resource\\report.txt";
    private static BankAccounts bankAccounts = new BankAccounts();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
         while (true) {
             System.out.println("\n银行账目管理系统");
             System.out.println("1. 显示所有账号");
             System.out.println("2. 查询账号");
             System.out.println("3. 添加账号");
             System.out.println("4. 更新账号");
             System.out.println("5. 删除账号");
             System.out.println("6. 导出打印文件");
             System.out.println("7. 退出");
             System.out.print("请选择操作: ");

             int choice = scanner.nextInt();
             scanner.nextLine();
             switch (choice) {
                 case 1:
                     displayAllAccounts();
                     break;
                 case 2:
                     searchAccount();
                     break;
                 case 3:
                     addAccount();
                     break;
                 case 4:
                     updateAccount();
                     break;
                 case 5:
                     deleteAccount();
                     break;
                 case 6:
                    bankAccounts.exportPrintFile(PRINT_FILE);
                     System.out.println("打印文件已生成: " + PRINT_FILE);
                     break;
                 case 7:
                     bankAccounts.saveAccounts();
                     System.out.println("系统已退出。");
                     return;
                 default:
                     System.out.println("无效选择，请重新输入。");
             }
         }
    }

    private static void displayAllAccounts() {
        List<BankAccount> accounts = bankAccounts.getAllAccounts();
        if (accounts.isEmpty()) {
            System.out.println("没有账号记录。");
            return;
        }

        System.out.printf("%-5s %-15s %-15s %-10s%n", "ID", "First Name", "Last Name", "Balance");
        System.out.println("--------------------------------------------");
        for (BankAccount bankAccount : bankAccounts.getAllAccounts()) {
            System.out.printf("%-5s %-15s %-15s %-10s%n",bankAccount.getId(),bankAccount.getFirstName(),bankAccount.getLastName(),bankAccount.getBalance());
        }
    }

    private static void searchAccount() {
        System.out.print("请输入要查询的账号ID: ");
        int id = scanner.nextInt();

        BankAccount account = bankAccounts.getAccount(id);
        if (account != null) {
            System.out.println("\n账号信息:");
            System.out.println("ID: " + account.getId());
            System.out.println("姓名: " + account.getFirstName() + " " + account.getLastName());
            System.out.println("余额: " + String.format("%.2f", account.getBalance()));
        } else {
            System.out.println("未找到ID为 " + id + " 的账号。");
        }
    }

    private static void addAccount() {
        System.out.println("\n添加新账号");
        System.out.print("请输入ID: ");
        int id = scanner.nextInt();
        scanner.nextLine();

        System.out.print("请输入名字: ");
        String firstName = scanner.nextLine();

        System.out.print("请输入姓氏: ");
        String lastName = scanner.nextLine();

        System.out.print("请输入初始余额: ");
        double balance = scanner.nextDouble();
        scanner.nextLine();

        BankAccount newAccount = new BankAccount(id, firstName, lastName, balance);
        if (bankAccounts.addAccount(newAccount)) {
            System.out.println("账号已成功添加。");
        } else {
            System.out.println("ID已存在，添加失败。");
        }
    }

    private static void updateAccount() {
        System.out.print("请输入要更新的账号ID: ");
        int id = scanner.nextInt();
        scanner.nextLine();

        BankAccount account = bankAccounts.getAccount(id);
        if (account == null) {
            System.out.println("未找到ID为 " + id + " 的账号。");
            return;
        }

        System.out.println("\n更新账号");

        System.out.print("请输入名字: ");
        String firstName = scanner.nextLine();

        System.out.print("请输入姓氏: ");
        String lastName = scanner.nextLine();

        System.out.print("请输入初始余额: ");
        double balance = scanner.nextDouble();
        scanner.nextLine();

        BankAccount newAccount = new BankAccount(id, firstName, lastName, balance);
        if (bankAccounts.updateAccount(account)) {
            System.out.println("账号信息已更新。");
        } else {
            System.out.println("更新失败。");
        }
    }

    private static void deleteAccount() {
        System.out.print("请输入要删除的账号ID: ");
        int id = scanner.nextInt();
        scanner.nextLine();

        if (bankAccounts.deleteAccount(id)) {
            System.out.println("账号已删除。");
        } else {
            System.out.println("未找到ID为 " + id + " 的账号。");
        }
    }
}
